<?
	ob_start();
	require_once('init.php');
	require_once('dataModel.php');
	require_once('cartClass.php');
	if(empty($_SESSION["UID"])){
		header("Location:login.php");
		ob_end_flush();
		exit;
	}
	if($_POST){
		if($_POST['action']=="addorder"){
			$iGID = intval($_POST['GID']);
			$iCID = intval($_POST['CID']);
			$iSID = $_POST['sizename'];
			$iNum = intval($_POST['txtnum']);
			$cartItem=unserialize($_SESSION['cart']);
			$iPrice=0; //用函数得到
			$iName="测试一"; //用函数得到
			$iKeyID=$iGID."^".$iCID."^".$iSID; 
			$gItem=array("$iKeyID","$iName","$iPrice" ,"$iNum");
			$cartItem->add($gItem);
			$items=$cartItem->getCart();
			$_SESSION['cart'] =serialize($cartItem);
		}elseif($_POST['action']=="submitorder"){
			if($_POST['action']=="submitorder"){
				if(!empty($_POST)){
					$orname = strval($_POST['orname']);
					$orphone = strval($_POST['orphone']);
					$oraddress = strval($_POST['oraddress']);
					$ordertotal = floatval($_POST['ordertotal']);
					$oruid = $_SESSION["UID"];
					/*添加信息到订单表*/
					$insertSQL = "INSERT INTO `order`(`orderid`,`oruid`,`orname`,`oraddress`,`orphone`,`ordertotal`,`orderremark`) ";
					$insertSQL .= "VALUES('GNY','$oruid','$orname','$oraddress','$orphone',$ordertotal,'')";
					$db = MySQL::getInstance('','r-w'); 
					$insertid = $DBR=$db->add($insertSQL); 
					/*修改订单ID*/
					$orderid = 'GNY-'.$insertid.'-'.$_SESSION["UID"].'-'.rand(1000,9999);
					$updateSQL = "UPDATE `order` SET `orderid`='$orderid' WHERE id = '$insertid' ";
					$db = MySQL::getInstance('','r-w'); 
					$insertid = $DBR=$db->update($updateSQL); 
					/*添加订单详细信息*/
					$count = count($_POST['goodid']);
					$exec_sql = "INSERT INTO `orderdetal`(`orderid`,`goodcolor`,`goodid`,`odnum`,`goodname`,`goodprice`,`goodtotal`) VALUES ";
					for($i=0;$i<$count;$i++){
						$goodid = $_POST['goodid'][$i];
						$odnum = $_POST['odnum'][$i];
						$goodsname = $_POST['goodsname'][$i];
						$goodprice = $_POST['goodprice'][$i];
						$goodtotal = $_POST['goodtotal'][$i];
						$goodcolor = $_POST['goodcolor'][$i];
						$exec_sql .= "('$orderid','$goodid','$goodcolor','$odnum','$goodsname','$goodprice','$goodtotal'),";
					}
					$exec_sql = substr($exec_sql, 0, -1);
					$db = MySQL::getInstance('','r-w'); 
					$insertid = $DBR=$db->add($exec_sql); 
				}
				$update_sql = "UPDATE `userinfo` SET ";
				$updateUserInfo = "";
				empty($_SESSION['realName'])?$updateUserInfo .= "`realname`='$orname',":'';
				empty($_SESSION['userAddress'])?$updateUserInfo .= "`userAddress`='$oraddress',":'';
				empty($_SESSION['telCode'])?$updateUserInfo .= "`telCode`='$orphone',":'';
				if(!empty($updateUserInfo)){
					$update_sql .= substr($updateUserInfo, 0, -1)." WHERE id = '$oruid' ";
					$db = MySQL::getInstance('','r-w'); 
					$insertid = $DBR=$db->update($updateSQL); 
					unset($_SESSION['cart']);
					header("Location:ordersuccess.php");
					ob_end_flush();
				}else{
					unset($_SESSION['cart']);
					header("Location:ordersuccess.php");
					ob_end_flush();
				}
			}
		}
	}
	
 	if(!isset($_SESSION['cart'])) 
 	{ 
		$_SESSION['cart'] =serialize(new Cart()); 
	}
  
  	$cartItem=unserialize($_SESSION['cart']);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
	<meta http-equiv="Content-Type" content="text/html; charset=<?=$lng_characset;?>" />
	<meta name="keywords" content="GreenCity,Shoes" />
	<meta name="description" content="GreenCity" />
	<meta name="copyright" content="Copyright (c) 2011 www.GreenCity.com All Rights Reserved." />
	<meta name="publisher" content="Copyright (c) 2010 www.GreenCity.com All Rights Reserved." />
	<meta name="author" content="GreenCity Team" />
	
	<TITLE><?=$AllPageTitle;?></TITLE> 

	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<link href="css/Buttonstyle.css" rel="stylesheet" type="text/css" />
	<link href="css/jquery.hiAlerts.css" rel="stylesheet" type="text/css" />
	<link href="css/body2.css" rel="stylesheet" type="text/css" />

	<? include("alljs.php"); ?>

	<script type="text/javascript" src="Scripts/jquery.js"></script>
	<script type="text/javascript" src="Scripts/jquery.jqzoom.js"></script>	

	<script language="javascript">
   		function removecart(GID,CID,SID,NUM)
   		{
   			$("form").submit(function(){
			   	var strSQL = "";                      
			   	strSQL+="iType=2";
			   	strSQL+="&GID="+GID;
			   	strSQL+="&CID="+CID;
			   	strSQL+="&SID="+SID;
			   	strSQL+="&NUM="+NUM;
	
				parent.hiConfirm('<?=$strdelcartyesno;?>', '<?=$strclueinfo;?>', function(r) 
				{       
					if (r) 
					{
						$.post("goodsCart.php",strSQL,function(msg)
						{
						   	if (msg=='1')
						   	{	
								parent.hiAlert("<?=$strdelcart;?>", '<?=$strclueinfo;?>');
								setTimeout('location.reload();parent.$("#popup_ok").click();',3000);
								return false;
						   	}
						})
					}	
				})	
				return false;
     		});
		}
					
   		function continueshop()
   		{
   			$("form").submit( function () {
   				self.location="body2.php";
     			return false;
     		});
		} 

   	   	//手机号码验证信息
		function isMobil(s) {
		    var patrn = /(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
		    if (!patrn.exec(s)) {
		        return false;
		    }
		    return true;
		} 
		
		function checksubmit()
		{
			var value = $("#orname").attr("value");
	       	if(value=="")
	       	{
				parent.hiAlert("<?=$lang['strinputusername'];?>", '<?=$strclueinfo;?>');
				$("#orname").focus();
				return false;
	        }

	       	var oraddress = $("#oraddress").attr("value");
	       	if(oraddress=="")
	       	{
				parent.hiAlert("<?=$lang['strinputaddress'];?>", '<?=$strclueinfo;?>');
				$("#oraddress").focus();
				return false;
	        }

	       	var orphone = $("#orphone").attr("value");
	       	if(orphone=="")
	       	{
				parent.hiAlert("<?=$lang['strinputphone'];?>", '<?=$strclueinfo;?>');
				$("#orphone").focus();
				return false;
	        }
	       	if(orphone!="")
	        {
	            if(!isMobil(orphone))
	            {
	            	parent.hiAlert("<?=$lang['strinputisphone'];?>", '<?=$strclueinfo;?>');
	                $("#orphone").focus();
	                return false;
	            }
	        }
		}   
	</script>

</head>
	<body>	
		<?
		include("header.php"); 
		?>

		<div id="body2_all" >
			<div id="body2_h01" ></div>
			
			<div class="cartdetail_div">
				<div class="cartdetail_a01"><b><?=$strmycart;?></b></div>	
				<form name="submitorder" action="./goodsorder.php" method="post" id="submitorder" onsubmit="return checksubmit()"> 
				<div class="cartdetail_a02">
					<div class="cartdetail_a04">
						<div class="cartdetail_a09" style="height:27px;line-height:27px;">
							<div class="cartdetail_a10" style="height:27px;line-height:27px;color:#629EB0"><b><?=$strProduct;?></b></div>
							<div class="cartdetail_a11" style="height:27px;line-height:27px;color:#629EB0"><b></b></div>
							<div class="cartdetail_a12" style="height:27px;line-height:27px;color:#629EB0"><b><?=$strSizeWidth;?></b></div>
							<div class="cartdetail_a13" style="height:27px;line-height:27px;color:#629EB0"><b><?=$stradviceprice;?></b></div>
							<div class="cartdetail_a14" style="height:27px;line-height:27px;color:#629EB0"><b><?=$strnum;?></b></div>
							<div class="cartdetail_a15" style="height:27px;line-height:27px;color:#629EB0"><b><?=$strSubTotal;?></b></div>
							<div class="cartdetail_a16" style="height:27px;line-height:27px;color:#629EB0"><b><?=$strRemove;?></b></div>
				    	</div>


						<?
						$Items=$cartItem->getCart();
						$dblAmt=0;  
						$dblNum=0;  
						foreach($Items as $iItem)
						{
							$iKeyIDs=$iItem['ID']; 
							$iKeyID= explode("^",$iKeyIDs);
							$iGID=$iKeyID[0];
							$iCID=$iKeyID[1];
							$iSID=$iKeyID[2];
							$iCount=$iItem['count'];
                            
                            $strSQL ="";

							$strSQL = "select g.id,g.goodsid,g.pic1,g.adviceprice,g.".$strLanguage_Goods.",c.".$strLanguage_Color.",s1.".$strLanguage_Sort1.",s2.".$strLanguage_Sort2." ";
							$strSQL.= "from goods g left join color c on c.id=g.colorid ";
							$strSQL.= "             left join sort1 s1 on s1.id=g.sort1id ";
							$strSQL.= "             left join sort2 s2 on s2.id=g.sort2id ";
							$strSQL.= "where g.id='".$iGID."' limit 0,1";
							
							$db = MySQL::getInstance('','r-w'); 
							$rsGoods = $db->fetch($db->query($strSQL));
							
							$iGoodsID=$rsGoods["goodsid"];
							$iColor=$rsGoods["colorname"];
							$iPic=$rsGoods["pic1"];
							$iAdvicePrice=$rsGoods["adviceprice"];
							$iAmt=$iAdvicePrice*$iCount;
							
							$dblAmt=$dblAmt+$iAmt; 
							$dblNum=$dblNum+$iCount;  
						?>
							<div class="cartdetail_a09" style="height:100px;line-height:27px;">

								<div class="cartdetail_a10">
									<img src="<?=$lng_picpath;?><?=$rsGoods["pic1"];?>" width="130" height="80" border="0" style="margin-top:5px; margin-left:5px;" />
								</div>
								<div class="cartdetail_a11">
									<div class="cartdetail_a20" style="margin-top:15px;"><?=$iColor;?><input type="hidden" name="color[]" value="<?=$iColor;?>" /></div>
									<div class="cartdetail_a20"><?=mb_strimwidth($rsGoods["goodsname"],0,16,'...');?><input type="hidden" name="goodsname[]" value="<?=$rsGoods["goodsname"];?>" /></div>
									<div class="cartdetail_a20"><?=$rsGoods["sort1name"];?><input type="hidden" name="sexgoodname[]" value="<?=$rsGoods["sort1name"];?>" /></div>
									<!--div class="cartdetail_a20"><?=$rsGoods["sort2name"];?></div-->
								</div>
								<div class="cartdetail_a12"><?=$iSID;?><input type="hidden" name="iSID[]" value="<?=$iSID;?>" /></div>
								<div class="cartdetail_a13">$ <?=$iAdvicePrice;?>.00<input type="hidden" name="goodprice[]" value="<?=$iAdvicePrice;?>" /></div>
								<div class="cartdetail_a14"><?=$iCount;?><input type="hidden" name="odnum[]" value="<?=$iCount;?>" /></div>
								<div class="cartdetail_a15">$ <?=$iAmt;?>.00<input type="hidden" name="goodtotal[]" value="<?=$iAmt;?>" /></div>
								<div class="cartdetail_a16" style="padding-top:32px;"><input type="hidden" name="goodid[]" value="<?=$iGID;?>" />
									<input type="image" name="remove" value="remove" src="images/<?=$lang['strremove']?>" onclick="javascript:removecart('<?=$iGID;?>','<?=$iCID;?>','<?=$iSID;?>','<?=$iCount;?>');" >
								</div>
					    	</div>
					    	
						<?	
						} 
						?>

						<div class="cartdetail_a09" style="height:27px;line-height:27px;margin-top:15px;">
							<div style="height:27px;line-height:27px;color:#629EB0;" class="cartdetail_a10"><b>收货人信息</b></div>
						</div>
						<div class="cartdetail_a09" style="height:27px;width:625px;line-height:27px;border-bottom:0px;margin-top:5px;text-align:left;padding-left:10px;">
							收货人姓名<i style="color:#ff0000;">*</i>
							<input maxlength="16" id="orname" name="orname" class="minput" type="text" value="<?=$_SESSION['realName']; ?>">
							<span id="receivingName_tips" class="mltips">
								<font class="f_999">（为避免日后退款时给您带来不便，请您使用真实姓名）</font>
							</span>
						</div>
						
						<div class="cartdetail_a09" style="height:27px;width:625px;line-height:27px;text-align:left;padding-left:10px;border-bottom:0px;">
							收货人手机<i style="color:#ff0000;">*</i>
                			<input id="orphone" name="orphone" maxlength="13" class="linput" style="width: 200px;" type="text" value="<?=$_SESSION['telCode']; ?>">
                			<span id="receivingAddress_tips" class="mltips"><font class="f_red">&nbsp;&nbsp;（请填写正确的手机号码，我们将会电话联系您。）</font></span>
						</div>
						
						<div class="cartdetail_a09" style="height:27px;width:625px;line-height:27px;text-align:left;padding-left:10px;">
							收货人地址<i style="color:#ff0000;">*</i>
                			<input id="oraddress" name="oraddress" maxlength="60" class="linput" style="width: 300px;" type="text" value="<?=$_SESSION['userAddress']; ?>">
               				<span id="receivingAddress_tips" class="mltips"><font class="f_red">&nbsp;&nbsp;（请填写详细地址，确保准确送达）</font></span>
						</div>


						<div class="cartdetail_a17">
							<div class="cartdetail_a18"><?=$strSubTotal.": ";?></div>
							<div class="cartdetail_a19"><?="$ ".$dblAmt.".00";?></div>
							<div class="cartdetail_a18"><?=$strSalesTax.": ";?></div>
							<div class="cartdetail_a19">$ 0.00</div>
							<div class="cartdetail_a18"><?=$strStdShipping.": ";?></div>
							<div class="cartdetail_a19">$ 0.00</div>
						</div>						

						<div class="cartdetail_a17" style="height:30px;">
							<div class="cartdetail_a18" style="font-size:16px;"><b><?=$strCURRENTTOTAL;?></b></div>
							<div class="cartdetail_a19" style="font-size:16px;"><b><?="$ ".$dblAmt.".00";?></b><input type="hidden" name="ordertotal" value="<?=$dblAmt;?>" /></div>
						</div>						

						<div class="cartdetail_a03">
							<input type="hidden" name="action" value="submitorder" />
							<input type="image" name="submit" value="submitorder" src="images/<?=$lang['strsubmitorder']?>">
							<input type="image" name="continueshop" value="continueshop" src="images/<?=$lang['strcontinueshop']?>" onclick="javascript:continueshop();" >
						</div>	
					</div>
					
					<div class="cartdetail_a05">
						<div class="cartdetail_a06"><?=$strcarthelp01;?></div>
						<div class="cartdetail_a07" style="margin-top:10px;"><b><?=$strcarthelp01;?></b></div>
						<div class="cartdetail_a07"><?=$strcarthelp03;?></div>

						<div class="cartdetail_a07" style="margin-top:15px;"><b><?=$strcarthelp04;?></b></div>
						<div class="cartdetail_a07"><?=$strcarthelp05;?></div>

						<div class="cartdetail_a07" style="margin-top:15px;"><b><?=$strcarthelp06;?></b></div>
						<div class="cartdetail_a07"><?=$strcarthelp07;?></div>

						<div class="cartdetail_a08" style="height:2px;margin-top:20px;border-top:1px solid #629EB0;"></div>
						<div class="cartdetail_a06"><?=$strcarthelp08;?></div>
						<div class="cartdetail_a07" style="color:#629EB0;font-size:12px;"><b><?=$strcarthelp09;?></b></div>
						<div class="cartdetail_a07" style="color:#629EB0;font-size:12px;"><b><?=$strcarthelp10;?></b></div>
						
					</div>
				</div>	
				</form>			
			</div>	

			<div id="body2_h02" ></div>
		</div>


		<?
		include("footer.php"); 
		?>
	</body>		
</html>